package com.transitionseverywhere.utils;

import android.animation.LayoutTransition;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import com.transitionseverywhere.R;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/* loaded from: classes.dex */
class ViewOverlayPreJellybean extends FrameLayout {
    private static final Field FIELD_VIEW_PARENT = ReflectionUtils.getPrivateField(View.class, "mParent");
    private List<Drawable> mDrawableOverlays;

    @Override // android.view.ViewGroup, android.view.View
    public boolean dispatchTouchEvent(MotionEvent motionEvent) {
        return false;
    }

    public ViewOverlayPreJellybean(Context context) {
        super(context);
        init();
    }

    public ViewOverlayPreJellybean(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        init();
    }

    public ViewOverlayPreJellybean(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
        init();
    }

    private void init() {
        this.mDrawableOverlays = new ArrayList();
    }

    @Override // android.view.ViewGroup
    public void addView(View view, int i, int i2) {
        LayoutTransition layoutTransition;
        Field field;
        if (view.getParent() instanceof ViewGroup) {
            ViewGroup viewGroup = (ViewGroup) view.getParent();
            if (viewGroup.getLayoutTransition() != null) {
                layoutTransition = viewGroup.getLayoutTransition();
                viewGroup.setLayoutTransition(null);
            } else {
                layoutTransition = null;
            }
            viewGroup.removeView(view);
            if (layoutTransition != null) {
                viewGroup.setLayoutTransition(layoutTransition);
            }
            if (view.getParent() != null) {
                ViewGroupUtils.cancelLayoutTransition(viewGroup);
                if (view.getParent() != null && (field = FIELD_VIEW_PARENT) != null) {
                    ReflectionUtils.setFieldValue(view, field, null);
                }
            }
            if (view.getParent() != null) {
                return;
            }
        }
        view.setTag(R.id.overlay_layout_params_backup, view.getLayoutParams());
        addView(view, initParams(view, i, i2));
        invalidate();
    }

    @Override // android.view.ViewGroup, android.view.ViewManager
    public void removeView(View view) {
        super.removeView(view);
        ViewUtils.setLayoutParamsSilently(view, (ViewGroup.LayoutParams) view.getTag(R.id.overlay_layout_params_backup));
        view.setTag(R.id.overlay_layout_params_backup, null);
    }

    public void moveView(View view, int i, int i2) {
        if (view.getParent() == this) {
            view.setLayoutParams(initParams(view, i, i2));
        }
    }

    private FrameLayout.LayoutParams initParams(View view, int i, int i2) {
        int[] iArr = new int[2];
        getLocationOnScreen(iArr);
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(-2, -2);
        int i3 = i - iArr[0];
        int i4 = i2 - iArr[1];
        layoutParams.leftMargin = i3;
        layoutParams.topMargin = i4;
        view.setLeft(i3);
        view.setTop(i4);
        if (view.getMeasuredWidth() != 0) {
            layoutParams.width = view.getMeasuredWidth();
            view.setRight(i3 + layoutParams.width);
        }
        if (view.getMeasuredHeight() != 0) {
            layoutParams.height = view.getMeasuredHeight();
            view.setBottom(i4 + layoutParams.height);
        }
        return layoutParams;
    }

    public synchronized void addDrawable(Drawable drawable) {
        if (drawable != null) {
            this.mDrawableOverlays.add(drawable);
            invalidate();
        }
    }

    public synchronized void removeDrawable(Drawable drawable) {
        this.mDrawableOverlays.remove(drawable);
        invalidate();
    }

    @Override // android.view.View
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        for (Drawable drawable : this.mDrawableOverlays) {
            drawable.draw(canvas);
        }
    }

    public static ViewOverlayPreJellybean getOverlay(ViewGroup viewGroup) {
        if (viewGroup != null) {
            ViewGroup viewGroup2 = viewGroup;
            while (viewGroup2.getId() != 16908290 && viewGroup2.getParent() != null && (viewGroup2.getParent() instanceof ViewGroup)) {
                viewGroup2 = (ViewGroup) viewGroup2.getParent();
            }
            for (int i = 0; i < viewGroup2.getChildCount(); i++) {
                View childAt = viewGroup2.getChildAt(i);
                if (childAt instanceof ViewOverlayPreJellybean) {
                    return (ViewOverlayPreJellybean) childAt;
                }
            }
            FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(-1, -1);
            layoutParams.gravity = 119;
            ViewOverlayPreJellybean viewOverlayPreJellybean = new ViewOverlayPreJellybean(viewGroup.getContext());
            viewGroup2.addView(viewOverlayPreJellybean, layoutParams);
            return viewOverlayPreJellybean;
        }
        return null;
    }
}
